package com.rs.game.player.content.construction;
 
import java.util.List;

import com.rs.game.DynamicRegion;
import com.rs.game.Region;
import com.rs.game.RegionBuilder;
import com.rs.game.World;
import com.rs.game.WorldObject;
import com.rs.game.item.Item;
import com.rs.game.player.Player;
 
public class House {
     
    private List<RoomReference> rooms;
    private byte place, look;
    private Player player;
     
    public House(Player player) {
    	this.player = player;
        rooms = player.getRooms();
        place = (byte) player.getPlace(); //desert
    }
     
    public void refresh(Player player, int[] boundChuncks) {
        player.setForceNextMapLoadRefresh(true);
        constructHouse(boundChuncks, true);
        player.loadMapRegions();
    }
    
    public void sendFurnitureInterface(Item[] items) {
    	player.getPackets().sendItems(396, items);
    }
     
    public void constructHouse(int[] boundChuncks, boolean buildMode) {
        Object[][][][] data = new Object[4][8][8][];
        for(RoomReference reference : rooms) {
            data[reference.getPlane()][reference.getX()][reference.getY()] =
                new Object[] {reference.getRoom().getChunkX(),  reference.getRoom().getChunkY(),
                    reference.getRotation(), reference.getRoom().isShowRoof()};
        }
        if(!buildMode) { //construct roof
            for(int x = 1; x < 7; x++) {
                skipY: for(int y = 1; y < 7; y++) {
                    for(int plane = 2; plane >= 0; plane--) {
                        if(data[plane][x][y] != null) {
                            boolean hasRoof = (boolean) data[plane][x][y][3];
                            if(hasRoof) {
                                byte rotation = (byte) data[plane][x][y][2];           
                                //TODO find best Roof
                                data[plane+1][x][y] = new Object[] {
                                        Roof.ROOF1.getChunkX(),
                                        Roof.ROOF1.getChunkY(), rotation, true};
                                continue skipY;
                            }
                        }
                    }
                }
            }
        }
        for(int plane = 0; plane < data.length; plane++) {
            for(int x = 0; x < data[plane].length; x++) {
                for(int y = 0; y < data[plane][x].length; y++) {
                    if(data[plane][x][y] != null)
                        RegionBuilder.copyChunk((int)data[plane][x][y][0] +
                                ((boolean)data[plane][x][y][3] && look >= 4 ? 8 : 0)
                                , (int)data[plane][x][y][1],
                                (boolean)data[plane][x][y][3] ? look % 4 : place
                                , boundChuncks[0]+x, boundChuncks[1]+y, plane,(byte)data[plane][x][y][2]);
                    else if (plane == 0)
                        RegionBuilder.copyChunk(LAND[0], LAND[1], place, boundChuncks[0]+x, boundChuncks[1]+y, plane, 0);
                }
            }
        }
        Region region = World.getRegion(RegionBuilder.getRegionHash(boundChuncks[0]/8, boundChuncks[1]/8));
        List<WorldObject> spawnedObjects = region.getSpawnedObjects();
        if(spawnedObjects != null)
            spawnedObjects.clear();
        List<WorldObject> removedObjects = region.getRemovedObjects();
        if(removedObjects != null)
            removedObjects.clear();
        for(RoomReference reference : rooms) {
            int boundX = reference.getX() * 8;
            int boundY = reference.getY() * 8;
//            int boundX1 = boundChuncks[0] * 8;
//            int boundY1 = boundChuncks[1] * 8;
            int chunkRotation = region.getRotation(reference.getPlane(), boundX, boundY);
            for(int x = boundX; x < boundX+8; x++) {
                for(int y = boundY; y < boundY+8; y++) {
                    WorldObject[] objects = region.getObjects(reference.getPlane(), x, y);
                    if(objects != null) {
                        for(WorldObject object : objects) {
                        	/*
                        	 * Remove the build hotspots (not 100%)
                        	 */
                            if(!buildMode && object.getDefinitions().containsOption("Build")) {
                                //object = new WorldObject(object.getId(), object.getType(), (object.getRotation() + chunkRotation) % 4, x + boundChuncks[0]*8,y + boundChuncks[1]*8, reference.getPlane());
                                if (object.getDefinitions().name.equalsIgnoreCase("door hotspot")) {
                                	//WorldObject object1 = new WorldObject(getDoorId(), object.getType(), object.getRotation() + 1, object.getX(), object.getY(), reference.getPlane());
                                		World.removeObject(object, true);
                                }
                                
                                else if (object.getDefinitions().name.equalsIgnoreCase("window space")) {
                                	//System.out.println(object.getId());
                                } else {
                                	World.removeObject(object, true);
                                }
                                try {
                                	WorldObject o = new WorldObject(13405, 10, 4, boundChuncks[0]*8 + 35, boundChuncks[1]*8 + 35, 0);
	                                World.spawnObject(o, true);
	                                for (WorldObject o1 : player.getConObjectsToBeLoaded()) {
	                                	//System.out.println(o1.getDefinitions().getOption(5));
	                                	World.spawnObject(o1, true);
	                                }
                                } catch (NullPointerException e) {
                                	e.printStackTrace();
                                }
                            }
                            /*
                             * Spawn the objects & the hotspots
                             */
                            else if (object.getDefinitions().name.equals("Window")) {
                                object = new WorldObject(getWindowId(), object.getType(), (object.getRotation() + chunkRotation) % 4, x + boundChuncks[0]*8,y + boundChuncks[1]*8, reference.getPlane());
                                World.spawnObject(object, true);
                            }
                            
                            else {
                            	try {
	                                for (WorldObject o1 : player.getConObjectsToBeLoaded()) {
	                                	World.spawnObject(o1, true);
	                                }
	                                WorldObject o = new WorldObject(13405, 10, 4, boundChuncks[0]*8 + 35, boundChuncks[1]*8 + 35, 0);
	                                World.spawnObject(o, true);
                                } catch (NullPointerException e) {
                                	System.out.println("Player objects are null");
                                	e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
     
    public void previewRoom(Player player, int[] boundChuncks, RoomReference reference, boolean remove) {
        int boundX = reference.getX() * 8;
        int boundY = reference.getY() * 8;
        Region region = World.getRegion(RegionBuilder.getRegionHash(reference.getRoom().getChunkX()/8, reference.getRoom().getChunkY()/8));
        int boundX2 = (reference.getRoom().getChunkX() - (reference.getRoom().getChunkX() / 8 * 8)) * 8;
        int boundY2 = (reference.getRoom().getChunkY() - (reference.getRoom().getChunkY() / 8 * 8)) * 8;
        DynamicRegion house = (DynamicRegion) World.getRegion(RegionBuilder.getRegionHash(player.getBoundChuncks()[0]/8, player.getBoundChuncks()[1]/8));
        house.getRegionCoords()[reference.getPlane()][reference.getX()][reference.getY()][3] = reference.getRotation();
        for(int x = 0; x < 8; x++) {
            for(int y = 0; y < 8; y++) {
                WorldObject[] objects = region.getObjects(reference.getPlane(), boundX2+x, boundY2+y);
                if(objects != null) {
                    for(WorldObject object : objects) {
                        if(object.getDefinitions().containsOption("Build")) {
                            int x2 = x;
                            int y2 = y;
                            for (int rotate = 0; rotate < reference.getRotation(); rotate++) {
                                int fakeChunckX = x2;
                                int fakeChunckY = y2;
                                x2 = fakeChunckY;
                                y2 = 7 - fakeChunckX;
                            }
                            object = new WorldObject(object.getId(), object.getType(), (object.getRotation() + reference.getRotation()) % 4, boundX + x2 + boundChuncks[0]*8, boundY + y2 + boundChuncks[1]*8, reference.getPlane());
                            if(remove)
                                player.getPackets().sendDestroyObject(object);
                            else
                                player.getPackets().sendSpawnedObject(object);
                        }
                    }
                }
            }
        }
    }
     
    public int getWindowId() {
        switch(place) {
        case 0:
        	return 13099;
        case 1:
        	return 13115;
        case 2:
        	return 0;
        case 3:
        	return 0;
        default:
            return 13115;
        }
    }
    
    public int getDoorId() {
    	switch(place) {
    	case 0:
    		return 13100;
    	default:
    		return 1058;
    	}
    }
    private static final int[] LAND = {233, 632};
     
}